package cloud.maque.sms.controller;

import cloud.maque.common.core.response.R;
import cloud.maque.sms.api.dto.SmsCodeDTO;
import cloud.maque.sms.api.dto.SmsDTO;
import cloud.maque.sms.api.dto.SmsResponse;
import cloud.maque.sms.conf.SmsProperties;
import cloud.maque.sms.service.SmsSendService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信发送接口，会做限流处理
 */
@RestController
@RequestMapping("/sms")
public class SmsController {


    @Autowired
    SmsSendService smsSendService;

    @Autowired
    SmsProperties smsProperties;

    /**
     * 发送短信
     *
     * @param request
     * @return
     */
    @PostMapping("/send")
    public R<SmsResponse> send(@RequestBody SmsDTO request) {
        SmsResponse smsResponse = smsSendService.send(smsProperties.getProvider(), request);
        return R.ok("success", smsResponse);
    }

    /**
     * 发送短信验证码
     *
     * @param dto
     * @return
     */
    @PostMapping("/send-code")
    public R<SmsResponse> sendCode(@RequestBody SmsCodeDTO dto) {
        SmsDTO request = new SmsDTO();
        BeanUtil.copyProperties(dto, request);
        request.setTemplateText(smsProperties.getCodeTemplate());
        request.setTemplateCode(smsProperties.getCodeTemplate());

        SmsResponse smsResponse = smsSendService.send(smsProperties.getProvider(), request);
        return R.ok("success", smsResponse);
    }
}
